package mercury;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.PropertyConfigurator;

import mercury.dao.CatalogDAOInterface;
import mercury.dao.CatalogException;
import mercury.dao.JDBCCatalog;
import mercury.util.Log;

public class AbstractCatalogProperties {

	protected CatalogDAOInterface catalog;
	private Log logger = new Log(this);
	
	public AbstractCatalogProperties(Map daoprops) {
		init(daoprops);
	}
	
	public AbstractCatalogProperties() {
		
	}

	public void init(Map daoprops) {
		String daoType = (String) daoprops.get("dao");
		
		if(daoType != null && daoType.equals("jdbc")) {
			try {
				catalog = new JDBCCatalog(daoprops);
			} catch (CatalogException e) {
				logger.error(e);
	        	logger.trace(e,e);
			}
		}else if(daoType != null && daoType.equals("http")) {
			try {
				catalog = new JDBCCatalog(daoprops);
			} catch (CatalogException e) {
				logger.error(e);
	        	logger.trace(e,e);
			}
		} else {
			logger.error("DAO type not specified correctly");
		}
	}
	
	private void update(String table,Map cols) {
		Map upd = new HashMap();
		
		Object[] a = cols.keySet().toArray();
		for(int i=0;i<a.length;i++) {
			String key = (String) a[i];
			String val = (String) cols.get(key);
			if(!key.equals("id") && !key.equals("table")) {
				upd.put("col:"+key,val);
			}
		}
		
		upd.put("table",table);
		upd.put("where:id",cols.get("id"));
		
		try {
			catalog.update(upd);
		} catch (CatalogException e) {
			logger.error(e);
        	logger.trace(e,e);
		}
	}
	
	private void insert(String table,Map props) {
		Map insrt = new HashMap();
		
		Object[] a = props.keySet().toArray();
		for(int i=0;i<a.length;i++) {
			String key = (String) a[i];
			String val = (String) props.get(key);
			if(!key.equals("id") && !key.equals("table")) {
				insrt.put("col:"+key,val);
			}
		}
		
		insrt.put("table",table);
		
		try {
			catalog.insert(insrt);
		} catch (CatalogException e) {
			logger.error(e);
        	logger.trace(e,e);
		}
	}
	
	public static void main(String args[]) {

		String loggerProps =  "D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\AppServerAdmin\\WEB-INF\\xml\\logger-console.properties";
		PropertyConfigurator.configure(loggerProps);
		
		// Test
		Map prms = new HashMap();
		prms.put("driver","com.microsoft.jdbc.sqlserver.SQLServerDriver");
		prms.put("url","jdbc:microsoft:sqlserver://");
		prms.put("server","localhost");
		prms.put("port","1433");
		prms.put("database","APPSERVFRMK");
//		 Informs the driver to use server a side-cursor, 
	     // which permits more than one active statement 
	     // on a connection.
		prms.put("selectmethod","cursor");
		prms.put("uid","sa");
		prms.put("pwd","");
		prms.put("dao","jdbc");

		//FrameworkProperties props = new FrameworkProperties(prms);
		//System.out.println(props.getServerProps("1"));
		//System.out.println(props.getAllServersXMLFormat("tristan"));
		//System.out.println(props.getJobsXMLFormat("1"));
		
		//String job = props.insertJob("1","tristan","testJob","this is desc");
		//props.deleteJob(job);

	}
}
